package com.ruoyi.web.utils;

import lombok.extern.slf4j.Slf4j;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class TimeFormatUtils {

	public static final String PATTERN_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	public static final String PATTERN_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

	public static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd";

	public static final String PATTERN_YYYY_MM = "yyyy-MM";

	public static final String PATTERN_MM_DD = "MM-dd";

	public static final String PATTERN_HH_MM_SS = "HH:mm:ss";

	public static String getNowWithPattren(String pattren){
		return format(pattren,new Date());
	}

	public static String getNowWithYMD(){
		return format(PATTERN_YYYY_MM_DD,new Date());
	}

	public static String getNowWithMD(){
		return format(PATTERN_MM_DD,new Date());
	}

	public static String getNowWithYMDHM(){
		return format(PATTERN_YYYY_MM_DD_HH_MM,new Date());
	}

	public static String getNowWithYMDHMS(){
		return format(PATTERN_YYYY_MM_DD_HH_MM_SS,new Date());
	}

	/**
	 * 格式化时间
	 * @param pattern
	 * @param date
	 * @return
	 */
	public static String format(String pattern, Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	/**
	 * 格式化时间
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String formatYYYYMMDD(Date date){
		return format(PATTERN_YYYY_MM_DD,date);
	}

	/**
	 * 格式化时间
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String formatMMDD(Date date){
		return format(PATTERN_MM_DD,date);
	}

	/**
	 * 格式化时间
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String formatYYYYMMDDHHMMSS(Date date){
		return format(PATTERN_YYYY_MM_DD_HH_MM_SS,date);
	}


	public static String formatYYYYMMDDHHMM(Date date){
		return format(PATTERN_YYYY_MM_DD_HH_MM,date);
	}

	/**
	 * 解析时间
	 * @param timeStr
	 * @return
	 * @throws ParseException
	 */
	public static Date parseYYYYMMDD(String timeStr){
		Date parse = null;
		try {
			parse = parse(PATTERN_YYYY_MM_DD, timeStr);
		}catch (ParseException e){
			log.error("ParseException:",e);
		}
		return parse;
	}

	/**
	 * 解析时间
	 * @param timeStr
	 * @return
	 * @throws ParseException
	 */
	public static Date parseYYYYMMDDHHMMSS(String timeStr){
		Date parse = null;
		try {
			parse = parse(PATTERN_YYYY_MM_DD_HH_MM_SS, timeStr);
		}catch (ParseException e){
			log.error("ParseException:",e);
		}
		return parse;
	}

	public static Date parseYYYYMMDDHHMM(String timeStr){
		Date parse = null;
		try {
			parse = parse(PATTERN_YYYY_MM_DD_HH_MM, timeStr);
		}catch (ParseException e){
			log.error("ParseException:",e);
		}
		return parse;
	}

	/**
	 * 解析时间
	 * @param pattern
	 * @param timeStr
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String pattern, String timeStr)throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.parse(timeStr);
	}

	/**
	 * date 转 timestamp
	 * @param data
	 * @return
	 */
	public static Timestamp date2Timestamp(Date data){
		return new Timestamp(data.getTime());
	}

	/**
	 * 对时间进行增加操作
	 * @param now 当前时间
	 * @param type 增加类型{@link Calendar}
	 * @param addNumber 加多少 (减就传负数!)
	 * @return
	 */
	public static Date addByTypeAndNumber(Date now, int type, int addNumber){
		Calendar calendar = getCalender(now);
		calendar.set(type,calendar.get(type)+addNumber);
		return calendar.getTime();
	}

	/**
	 * 获取某月某天凌晨零点零分零秒
	 * @param dayOfMonth 1.月初
	 * @param addMonth 向前某月
	 * @return
	 */
	public static Date getDayOfMonthAndMonth(Date date, int dayOfMonth,int addMonth){
		Calendar calendar = getCalender(date);
		calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH) + addMonth);
		calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
		return getZeroOfDay(calendar);
	}

	/**
	 * 获取当天0点0分0秒
	 * @param calendar
	 * @return
	 */
	public static Date getZeroOfDay(Calendar calendar){
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取当天0点0分0秒
	 * @param date
	 * @return
	 */
	public static Date getZeroOfDay(Date date){
		return getZeroOfDay(getCalender(date));
	}

	/**
	 * 获取calender实例
	 * @param date
	 * @return
	 */
	public static Calendar getCalender(Date date){
		Calendar calendar = getCalender();
		calendar.setTime(date);
		return calendar;
	}

	/**
	 * 获取calender实例
	 * @return
	 */
	public static Calendar getCalender(){
		Calendar calendar = Calendar.getInstance();
		return calendar;
	}
	/**
	 * 获取指定年月的第一天
	 * @param ym yyyy-MM
	 * @return
	 */
	public static String getFirstDayOfMonth(String ym) {
		String arr[] = ym.split("-");
		int year = Integer.parseInt(arr[0]);
		int month = Integer.parseInt(arr[1]);
		Calendar cal = Calendar.getInstance();
		//设置年份
		cal.set(Calendar.YEAR, year);
		//设置月份
		cal.set(Calendar.MONTH, month - 1);
		//获取某月最小天数
		int firstDay = cal.getMinimum(Calendar.DATE);
		//设置日历中月份的最小天数
		cal.set(Calendar.DAY_OF_MONTH, firstDay);
		//格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(cal.getTime());
	}

	/**
	 * 获取指定年月的最后一天
	 * @param ym
	 * @return
	 */
	public static String getLastDayOfMonth(String ym) {
		String arr[] = ym.split("-");
		int year = Integer.parseInt(arr[0]);
		int month = Integer.parseInt(arr[1]);
		Calendar cal = Calendar.getInstance();
		//设置年份
		cal.set(Calendar.YEAR, year);
		//设置月份
		cal.set(Calendar.MONTH, month - 1);
		//获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DATE);
		//设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		//格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(cal.getTime());
	}

	/**
	 * 时间格式转换
	 * @param date 时间
	 * @param formPattern 原格式
	 * @param toPattern 转为新格式
	 * @return
	 * @throws ParseException
	 */
	public static String transferTimeFormatter(String date,String formPattern,String toPattern)throws ParseException{
		return format(toPattern,parse(formPattern,date));
	}

}
